package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.wcs.dao.StnMappingDao;
import com.cxyd.modules.wcs.dto.StnMappingDTO;
import com.cxyd.modules.wcs.entity.StnMappingEntity;
import com.cxyd.modules.wcs.service.StnMappingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StnMappingServiceImpl extends CrudServiceImpl<StnMappingDao, StnMappingEntity, StnMappingDTO> implements StnMappingService {
    @Override
    public QueryWrapper<StnMappingEntity> getWrapper(Map<String, Object> params) {
        String wmsStation = (String) params.get("wms_station");
        final QueryWrapper<StnMappingEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(wmsStation), "wms_station", wmsStation);
        return wrapper;
    }

    @Override
    public void generateStnMapping(int row, int column, int level) {
        for (int r = 1; r <= row; r++) {
            for (int c = 1; c <= column; c++) {
                for (int l = 1; l <= level; l++) {
                    StnMappingEntity map = new StnMappingEntity();
                    map.setWmsStation(String.format("HW-01K%02d%02d%02d", r, c, l));
                    map.setWcsStation(String.format("HW-01K%02d%02d%02d", r, c, l));
                    baseDao.insert(map);
                }
            }
        }
    }

    @Override
    public StnMappingEntity getStnByNo(String StnNo) {
        QueryWrapper<StnMappingEntity> qw = new QueryWrapper<>();
        qw.eq("wms_station", StnNo).last("limit 1");
        return baseDao.selectOne(qw);
    }
}
